﻿using Furion.LinqBuilder;
using Furion.Localization;
using ST.Torch.WebApi.Application.DaTi.Dtos;
using ST.Torch.WebApi.Application.DaTi.Services;
using ST.Torch.WebApi.Core.Entities.DaTi;
using System.Data;
using System.Linq.Expressions;
using MiniExcelLibs;
using ST.Torch.WebApi.Application.AOP;

namespace ST.Torch.WebApi.Application.DaTi
{

    /// <summary>
    /// 公司代码接口
    /// </summary>
    [AppAuthorize]
    public class D_UserInforAppService : IDynamicApiController
	{
		private readonly ID_UserInforService _D_UserInforService;

        public D_UserInforAppService(ID_UserInforService D_UserInforservice)
        {
            _D_UserInforService = D_UserInforservice;
        }


        [HttpPost]
		public async Task<PagedList<D_UserInforListDto>> GetDataPagedList([FromForm] PageReq req)
		{
			Expression<Func<D_UserInfor, bool>> predicate = u => true;
            predicate=predicate.AndIf(!string.IsNullOrEmpty(req.key),u=>u.OpenId.Contains(req.key));
			
			var dataList= await _D_UserInforService.QueryPageAsync(predicate, req);
			PagedList<D_UserInforListDto> resultList = dataList.Adapt<PagedList<D_UserInforListDto>>();
			return resultList;
		}
        [HttpPost]
        public async Task<D_UserInforListDto> Single([FromForm] Guid Id)
        {
           
           var entity= await _D_UserInforService.SingleAsync(u=>u.Id==Id);
            D_UserInforListDto resultData = entity.Adapt<D_UserInforListDto>();
			return resultData;

        }
        [HttpPost]
        [OperateAuth("GeneralAddDataPerm")]
        public async Task Insert([FromForm] D_UserInforDto model)
        {
            D_UserInfor entity = model.Adapt<D_UserInfor>();
            await _D_UserInforService.InsertAsync(entity);
        }
		[HttpPost]
        [OperateAuth("GeneralEditDataPerm")]
		public async Task Update([FromForm] D_UserInforListDto model)
		{
			
			var entity=await _D_UserInforService.SingleAsync(u => u.Id == model.Id);
			
            entity.OpenId=model.OpenId;
            entity.C_Code=model.C_Code;
            entity.C_Name=model.C_Name;
            entity.G_Name=model.G_Name;
            entity.G_ImgUrl=model.G_ImgUrl;
            await _D_UserInforService.UpdateAsync(entity);
		}
		[HttpPost]
        [OperateAuth("DelD_UserInforPage")]
		public async Task DeleteAsync([FromForm] Guid Id)
		{
			var entity = await _D_UserInforService.SingleAsync(u => u.Id == Id);
			await _D_UserInforService.DeleteAsync(entity,true);
		}

        [HttpPost]
        [OperateAuth("DelD_UserInforPage")]
        public async Task DeleteBatchAsync([FromForm] List<Guid> Ids)
        {
            var entitys = await _D_UserInforService.QueryAsync(u => Ids.Contains(u.Id));
            foreach (var entity in entitys)
            {
                await _D_UserInforService.DeleteAsync(entity, true);
            }
        }

        /// <summary>
        /// 导出记录Excel
        /// </summary>
        /// <returns></returns>
        [HttpGet, QueryParameters, NonUnify]
        [OperateAuth("GeneralExportDataPerm")]
        public async Task<dynamic> ExPortExcel(string key)
        {
            Expression<Func<D_UserInfor, bool>> predicate = u => true;
            predicate = predicate.AndIf(!string.IsNullOrEmpty(key), u => u.OpenId.Contains(key));
            var dataList= await _D_UserInforService.QueryAsync(predicate);
            var ExData = dataList.Adapt<List<D_UserInforDto>>();
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(ExData);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "D_UserInfor"+DateTime.Now.ToString("HHmmss")+".xlsx"
            };

        }

     


       


    }

}
